package org.dromara.visitor.controller;

import jakarta.annotation.Resource;
import org.dromara.common.core.domain.R;
import org.dromara.visitor.domain.dto.ApplyAndVisitMonthResp;
import org.dromara.visitor.domain.dto.ApplyMonthStatusResp;
import org.dromara.visitor.domain.dto.ApplyStatisticsSummaryResp;
import org.dromara.visitor.service.ApplyService;
import org.dromara.visitor.service.BlacklistService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/vis/report")
public class ApplyReportController {
    @Resource
    private ApplyService applyService;

    @Resource
    private BlacklistService blacklistService;

    /**
     * 今日统计汇总数据
     * @return
     */
    @GetMapping("/toDaySummary")
    public R<ApplyStatisticsSummaryResp> toDaySummary(){
        ApplyStatisticsSummaryResp resp = applyService.toDaySummary();
        Long totalCount = blacklistService.count();
        resp.setTotalBlacks(totalCount);
        return R.ok(resp);
    }

    /**
     * 本月审批状态占比
     * @return
     */
    @GetMapping("/monthStatus")
    public R<ApplyMonthStatusResp> monthStatusRate(){
        ApplyMonthStatusResp resp = applyService.monthStatusRate();
        return R.ok(resp);
    }

    /**
     * 访客申请&实际到访数量统计
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/applyAndVisitMonth")
    public R<Map<String,List<ApplyAndVisitMonthResp>>> applyAndVisitMonth(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        Map<String, List<ApplyAndVisitMonthResp>> map = new HashMap<>();
        LocalDateTime start = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atTime(0, 0, 0);
        LocalDateTime end = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atTime(23, 59, 59);
        List<ApplyAndVisitMonthResp>  list = applyService.applyAndVisitMonth(start,end);
        map.put("apply",list);
        map.put("visit",new ArrayList<>());
        return R.ok(map);
    }
}
